<template>
    <div class="common-files-upload">
        <Row v-for="(item,index) in showFilesList" :key="index" class="common-files-upload-row">
            <Col span="24">
                <Tooltip :content="item.desc" placement="top-start" v-if="item.desc" max-width="400">
                    <Icon type="md-help-circle" class="icon-weizhi"/>
                </Tooltip>
                <FormItem prop="panorama_img" class="form-item-weizhi" :label="item.name">
                    <div class="form-element">
                        <wl-upload-oss-files
                                v-model="item.value"
                                :maxNum="50"
                                uploadType="hadoop"
                                :uploadUrl="getUploadUrl"
                                fileName="origin"
                                :filterReturn="item.key"
                                uploadStyle="other"
                                :beforeUploadFilter="uploadFilter"
                                @result="resultAction"
                                :format="format">
                        </wl-upload-oss-files>
                    </div>
                </FormItem>
                <Divider dashed class="divider"/>
            </Col>
        </Row>
    </div>
</template>

<script>

import { isNumber } from '@/libs/util'

export default {
  name: 'CommonFilesUpload',
  data () {
    return {
      tplfileList: [],
      format: ['jpg', 'jpeg', 'png', 'pdf', 'doc', 'excel']
    }
  },
  props: {
    showFilesList: {
      type: Array,
      default: () => {
        return []
      }
    },
    type: {
      type: String,
      default: () => {
        return ''
      }
    },
    dir: {
      type: String,
      default: () => {
        return 'house'
      }
    }
  },
  model: {
    prop: 'defaultFilesList',
    event: 'change'
  },
  computed: {
    getUploadUrl () {
      return this.$config.upLoadFileUrl
    }
  },
  methods: {
    filterData () {
      let resultList = []
      if (this.defaultFilesList === '') {
        this.tplfileList.forEach(item => {
          resultList.push({
            name: item.file_name,
            key: item.file_id + '_' + this.$common.xeUtils.toDateString(new Date(), 'yyyy.MM.dd') + '_',
            value: '',
            dir: this.dir
          })
        })
        this.showFilesList = resultList
      } else {
        this.showFilesList = evil(this.defaultFilesList)
      }
    },
    uploadFilter (file, uploadList, filterReturn) {
      let isExit = false
      if (uploadList.length > 0) {
        uploadList.forEach(item => {
          if (item.url.indexOf(file.name) !== -1) {
            isExit = true
          }
        })
      }
      if (isExit) {
        this.$Notice.warning({
          title: file.name + '【文件已经存在】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      let arr = file.name.split('.')
      let arr2 = arr[0].split('-')
      if (arr2.length !== 4 || !isNumber(arr2[0]) || !isNumber(arr2[1]) || !isNumber(arr2[2]) || !isNumber(arr2[3])) {
        this.$Notice.warning({
          title: file.name,
          desc: '文件命名格式不正确必须全部是数字包含三个"-"',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[0]) !== filterReturn.a) {
        this.$Notice.warning({
          title: file.name + '【文件一级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[1]) !== filterReturn.b) {
        this.$Notice.warning({
          title: file.name + '【文件二级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[2]) < 1 || parseInt(arr2[2]) > 10) {
        this.$Notice.warning({
          title: file.name + '【文件三级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      if (parseInt(arr2[3]) < 1 || parseInt(arr2[3]) > 100) {
        this.$Notice.warning({
          title: file.name + '【文件四级类别不正确】',
          duration: 0
        })
        return { errcode: false, selfShow: true }
      }
      return { errcode: true }
    },
    resultAction () {
      this.$emit('change', JSON.stringify(this.showFilesList))
      this.$emit('result', this.showFilesList)
    }
  },
  mounted () {
    // this.getJieKou()
  }
}
</script>

<style scoped lang="less">
    .common-files-upload {
        &-row {
            /*height: 46px;*/
        }

        &-aline {
            display: flex;
            height: 100%;
            align-items: center;
        }

        &-oss {
            display: flex;
            height: 100%;
            align-items: center;
        }

        &-oss > div {
            display: flex;
        }
    }

    .common-files-upload /deep/ .ivu-divider-horizontal {
        margin: 0 0 10px 0;
    }

    .icon-weizhi {
        position: absolute;
        left: 0px;
        top: 5px;
        color: orange;
        cursor: pointer;
    }

    .form-item-weizhi {
        margin-left: 15px;
    }
</style>
